package com.mhkj.statis.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.functionOffice.entity.FunctionOffice;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.region.service.NationService;
import com.mhkj.common.util.ItemHelper;
import com.mhkj.revise.plan.entity.Plan;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mhkj.revise.plan.service.PlanService;
import com.mhkj.statis.vo.StatisListVo;
import com.mhkj.statis.vo.TreePlanVo;
import com.mics.core.common.util.json.JsonUtils;
import com.mics.core.system.organizational.bean.Company;
import com.mics.core.system.organizational.bean.CompanyData;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 统计列表controller
 * @author wt
 */
@Controller
@RequestMapping(value = "/statis/list")
public class StatisListController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private NationService nationService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private PlanService planService;
	@Autowired
	private PlanNodeService planNodeService;
	@Resource
	private ItemHelper itemHelper;
	
	/**
	 *统计
	 * @throws Exception 
	 */
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public StatisListVo listByPage( Long month, Long planId,Long year,String code,Long functionOffice) throws Exception {
		
		
		Long planTypeId = null;
		if(!StringUtils.isEmpty(code)) {
			Dictionary byCode = dictionaryService.getByCode(code);
			 planTypeId = byCode.getId();
		}
		
		
		List<String> listMonth = new ArrayList<String>(); //月份
		List<Integer> listMonthAllTask = new ArrayList<Integer>(); //每月份总数
		List<Integer> listOver = new ArrayList<Integer>();//完成
		List<Integer> listNoOver = new ArrayList<Integer>();//未完成
		List<Integer> listOverdue = new ArrayList<Integer>();//逾期
		List<Double> listOverOnTime = new ArrayList<Double>();//按时完成率
		 for(int i=1;i<=12;i++) {
			 listMonth.add(i+"月");
			 listMonthAllTask.add(0);
			 listOver.add(0);
			 listNoOver.add(0);
			 listOverdue.add(0);
			 listOverOnTime.add(0d);
		 }
		 DecimalFormat dFormat=new DecimalFormat("#.00");
		StatisListVo vo = new StatisListVo();
		
		if(!StringUtils.isEmpty(month)) {
			
			DateTime dateTime = new DateTime(month);
			int monthOfYear = dateTime.getMonthOfYear();
			

		// 获取所有符合条件的任务总数(年,月)
		List<PlanNode> listTaskAll = planNodeService.getAllTask(planId,month,year,planTypeId,code,functionOffice);
		vo.setAllTaskNum(listTaskAll.size());
		//应当完成数(年,月)(结束时间在或者延期结束时间在时间范围内)
		List<PlanNode> listShouldCompleted = planNodeService.getShouldCompletedTask(planId,month,year,planTypeId,code,functionOffice);
		vo.setShouldCompleted(listShouldCompleted.size());
		//按时完成的(在应当完成的基础是判断完成时间<结束时间)
		List<PlanNode> listCompletedOnTimeTask = planNodeService.getCompletedOnTimeTask(planId,month,year,planTypeId,code,functionOffice);
		//完成的(在总任务的基础上判断完成时间不为空的)
		List<PlanNode> listCompletedTask = planNodeService.getCompletedTask(planId,month,year,planTypeId,code,functionOffice);
		//完成数 
		vo.setOverNum(listCompletedTask.size());
		// 总任务数-完成的 = 未完成的
		int comp = listTaskAll.size()-listCompletedTask.size();
//==============		
//		vo.setNoOverNum(comp);
		//逾期,查询逾期条数大于0的
		List<PlanNode> listOverdueTask = planNodeService.getOverdue(planId,month,year,planTypeId,code,functionOffice);
		vo.setOverdueNum(listOverdueTask.size());
		vo.setNoOverNum(comp-listOverdueTask.size());		
		//按时完成率(按时完成的/应当完成)
		
		Double d = 0d;
		if(listShouldCompleted.size()>0) {
			int size = listCompletedOnTimeTask.size();
			int size2 = listShouldCompleted.size();
			d = size*100d/size2;
	          String yearString =dFormat.format(d);
	          d= Double.valueOf(yearString);
		} 
		 listMonthAllTask.set(monthOfYear-1, listTaskAll.size());
		 listOver.set(monthOfYear-1, listCompletedTask.size());
		 listNoOver.set(monthOfYear-1,comp-listOverdueTask.size());
		 listOverdue.set(monthOfYear-1,listOverdueTask.size());
		 listOverOnTime.set(monthOfYear-1,d);
		
		}else {
			// 获取所有符合条件的任务总数(年,月)
			List<PlanNode> listTaskAll = planNodeService.getAllTask(planId,month,year,planTypeId,code,functionOffice);
			vo.setAllTaskNum(listTaskAll.size());
			//应当完成数(年,月)(结束时间在或者延期结束时间在时间范围内)
			List<PlanNode> listShouldCompleted = planNodeService.getShouldCompletedTask(planId,month,year,planTypeId,code,functionOffice);
			vo.setShouldCompleted(listShouldCompleted.size());
			//按时完成的(在应当完成的基础是判断完成时间<结束时间)
			List<PlanNode> listCompletedOnTimeTask  = planNodeService.getCompletedOnTimeTask(planId,month,year,planTypeId,code,functionOffice);
			//完成的(在总任务的基础上判断完成时间不为空的)
			List<PlanNode> listCompletedTask = planNodeService.getCompletedTask(planId,month,year,planTypeId,code,functionOffice);
			//完成数 
			vo.setOverNum(listCompletedTask.size());
			// 总任务数-完成的 = 未完成的
			int comp = listTaskAll.size()-listCompletedTask.size();
//			vo.setNoOverNum(comp);
			//逾期,查询逾期条数大于0的
			List<PlanNode> listOverdueTask = planNodeService.getOverdue(planId,month,year,planTypeId,code,functionOffice);
			vo.setOverdueNum(listOverdueTask.size());
			//未完成出去逾期的
			vo.setNoOverNum(comp-listOverdueTask.size());	
			
			List<Long> list = new ArrayList<Long>();
			DateTime dateTime = new DateTime(year);
			int myyear = dateTime.getYear();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			
			for(int i =1 ;i<=12 ;i++) {
				String m = i<10?"-0"+i:"-"+i;
				String s =myyear+m+"-01";
				Long parse = sdf.parse(s).getTime();
				list.add(parse);
			}
		 	
			
			for(int j=0;j<12;j++) {
				month = list.get(j);
				List<PlanNode> listTaskAllmonth = planNodeService.getAllTask(planId,month,year,planTypeId,code,functionOffice);//当月总任务
				List<PlanNode> listShouldCompletedmonth = planNodeService.getShouldCompletedTask(planId,month,year,planTypeId,code,functionOffice);//应当完成的
				List<PlanNode> listCompletedOnTimeTaskmonth = planNodeService.getCompletedOnTimeTask(planId,month,year,planTypeId,code,functionOffice);//按时完成的
				List<PlanNode> listCompletedTaskmonth = planNodeService.getCompletedTask(planId,month,year,planTypeId,code,functionOffice);//完成的
				List<PlanNode> listOverdueTaskmonth = planNodeService.getOverdue(planId,month,year,planTypeId,code,functionOffice);//逾期的
				int qqq = listTaskAllmonth.size()-listCompletedTaskmonth.size();
				Double d = 0d;
				if(listShouldCompletedmonth.size()>0) {
					
					int size = listCompletedOnTimeTaskmonth.size();//按时完成数
					int size2 = listShouldCompletedmonth.size();//应当完成数
					
					d = size*100d/size2;
			          String yearString =dFormat.format(d);
			          d= Double.valueOf(yearString);

				} 
				 listMonthAllTask.set(j, listTaskAllmonth.size());
				 listOver.set(j, listCompletedTaskmonth.size());
				 listNoOver.set(j,qqq-listOverdueTaskmonth.size());
				 listOverdue.set(j,listOverdueTaskmonth.size());
				 listOverOnTime.set(j,d);
				
			}
			
			
		}
		
		
		
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
		HashMap<String, Object> map = new HashMap<String,Object>();
		map.put("listMonth", listMonth);
		map.put("listOver", listOver);
		map.put("listMonthAllTask", listMonthAllTask);
		map.put("listNoOver", listNoOver);
		map.put("listOverdue", listOverdue);
		map.put("listOverOnTime", listOverOnTime);
		list.add(map);
		vo.setList(list);
		return vo;

	}	
//	
//	/**
//	 * 统计查询条件专用树
//	 */
//	@RequestMapping(value = "/TreePlanVo", method = RequestMethod.POST)
//	@ResponseBody
//	public List<TreePlanVo> TreePlanVo(String code) {
//		Long planTypeId = null;
//		if(!StringUtils.isEmpty(code)) {
//			Dictionary byCode = dictionaryService.getByCode(code);
//			 planTypeId = byCode.getId();
//		}
//		 
//		CompanyData companiedData =  orgService.listCompanys();
//		if(companiedData!=null) {
//		List<Company> childCompanyList = companiedData.getChildCompanyList();
//		List<TreePlanVo> list = new ArrayList<TreePlanVo>();
//		TreePlanVo e = new TreePlanVo();
//		e.setId(companiedData.getHeadCompany().getId());
//		e.setName(companiedData.getHeadCompany().getCompanyname());
//		list.add(e);
//		for (Company company : childCompanyList) {
//			TreePlanVo en = new TreePlanVo();
//			String id = company.getId();
//			en.setId(id);
//			en.setPid(company.getParentid());
//			en.setName(company.getCompanyname());
//			List<Item>	listItem = itemService.getListItemByaffiliatedCompanyId(id,null,null);
//			for (Item item : listItem) {
//				TreePlanVo enit = new TreePlanVo();
//				Long itemId = item.getId();
//				enit.setId(itemId.toString());
//				enit.setPid(id);
//				enit.setName(item.getName());
//				list.add(enit);
//				List<Plan>  plan = planService.findPlanByItemId(itemId,planTypeId,code);
//				for (Plan pla : plan) {
//					TreePlanVo enp = new TreePlanVo();
//					enp.setId(pla.getId().toString());
//					enp.setPid(itemId.toString());
//					enp.setName(pla.getName());
//					enp.setType("plan");
//					list.add(enp);
//				}
//			}
//			list.add(en);
//		}
//		
//		return list;
//		}else {
//			return null;
//		}
//		
//	}	
//	
	
	
	
	
	/**
	 * 统计查询条件专用树
	 */
	@RequestMapping(value = "/TreePlanVo", method = RequestMethod.POST)
	@ResponseBody
	public List<TreePlanVo> TreePlanVo(String code) {
		Long planTypeId = null;
		if(!StringUtils.isEmpty(code)) {
			Dictionary byCode = dictionaryService.getByCode(code);
			 planTypeId = byCode.getId();
		}
		 
		List<TreePlanVo> list = new ArrayList<TreePlanVo>();
		 
		List<Item> listItem = itemHelper.getCurrentUserItems();
			for (Item item : listItem) {
				TreePlanVo enit = new TreePlanVo();
				Long itemId = item.getId();
				enit.setId(itemId.toString());
				enit.setPid(null);
				enit.setName(item.getName());
				list.add(enit);
				List<Plan>  plan = planService.findPlanByItemId(itemId,planTypeId,code);
				for (Plan pla : plan) {
					TreePlanVo enp = new TreePlanVo();
					enp.setId(pla.getId().toString());
					enp.setPid(itemId.toString());
					enp.setName(pla.getName());
					enp.setType("plan");
					list.add(enp);
				}
			}
			 return list;
		}
		
  
	/**
	 * 统计查询条件专用树
	 */
	@RequestMapping(value = "/functionOfficeList")
	@ResponseBody
	public void functionOfficeList(HttpServletResponse response,String name){
		
		List<FunctionOffice> functionOffices = itemHelper.getCurrentUserFunctionOffices();
		JsonUtils.sendJsonFromList(response, true, "获取数据成功!", functionOffices, new String[]{"id","name",}, "id","name");
		//List<Dictionary> listByName = dictionaryService.listByName(name);
		
//		return functionOffices;
	}
}
